<?php
/******************************************************
v4 - Management Framework
Copyright (C) 2008  Gary Taylor, gMerc Incorporation
http://www.gmerc.com/v4/

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

gary@gmerc.com
******************************************************/
class showinvoice {
  function loaddata() {
    global $masterclass;

    $masterclass->loadmodule('customers');
    $masterclass->loadmodule('personnel');
    $masterclass->loadmodule('invoices');
    $masterclass->loadmodule('companies');
    $masterclass->loadmodule('payments');
    $masterclass->loadmodule('captcha',$masterclass->mainconfig['v4rootdir'].'lib/captcha.lib.php');
    
    $masterclass->loadjavascript('invoices');
    
    $masterclass->loadjavascript('',$masterclass->mainconfig['v4rootdir'].'include/suckertreemenu.inc.js');

    $masterclass->websiteconfig['title']='Invoice';
    $tmpdocid=$masterclass->allclasses['invoices']->fetch_invoicesdocumentid($masterclass->sitevars['invoicesid']);
    $tmprecipient=$masterclass->allclasses['invoices']->fetch_invoicesrecipient($masterclass->sitevars['invoicesid']);
    if ($tmpdocid!='') { $masterclass->websiteconfig['title'].=' #'.$tmpdocid; }
    if ($tmprecipient!='') { $masterclass->websiteconfig['title'].=' - '.$tmprecipient; }
    
    $masterclass->site['postfields'][] = array(
      'table'=>'invoices_documents',
      'trackingid'=>'invoicesid',
      'fields'=>array(
        'documentid'=>'invoices_documentid',
        'documentdate'=>'invoices_documentdate',
        'duedate'=>'invoices_documentduedate',

        'net'=>'invoices_net',
        'paid'=>'invoices_paid',

        'recipientlineone'=>'invoices_recipientlineone',
        'recipientlinetwo'=>'invoices_recipientlinetwo',
        'recipientlinethree'=>'invoices_recipientlinethree',
        'recipientlinefour'=>'invoices_recipientlinefour',
        'recipientlinefive'=>'invoices_recipientlinefive',
        'companylineone'=>'invoices_companylineone',
        'companylinetwo'=>'invoices_companylinetwo',
        'companylinethree'=>'invoices_companylinethree',
        'companylinefour'=>'invoices_companylinefour',
        'companylinefive'=>'invoices_companylinefive',
        
        'customer'=>'customerid'
      )
    );
    $masterclass->site['postfields'][] = array(
      'trackingid'=>'customerid'
    );
    
    if ($masterclass->allclasses['invoices']->fetch_invoicesdocumenttype($masterclass->sitevars['invoicesid'])=='quote') {
      header("Location: ".$masterclass->mainconfig['dir']."invoices/quote.php?".$masterclass->trackingvar_query());
      exit();
    } else if ($masterclass->checkid('`invoices_documents`',$masterclass->sitevars['invoicesid'])=='') {
      echo '<div id="error_overlay"><div>INVOICE DOES NOT EXIST:<br><input type="button" OnClick="window.close();" value=" Close Window " class="button"></div></div>';
      exit();
    }
    
    $masterclass->loadtrackingvalues_table('invoices_documents',$masterclass->sitevars['invoicesid']);
    
    if ($masterclass->sitevars['invoicesid']!='') {
      
      $masterclass->site['invoices_paymenttotals']=$masterclass->allclasses['payments']->calc_payment_totals($masterclass->sitevars['invoicesid']);
      
      $masterclass->site['invoices_documenttotals']=$masterclass->allclasses['payments']->calc_payment_documents($masterclass->sitevars['invoicesid']);
      $masterclass->site['invoices_paymenttotals']['totalpayments']+=$masterclass->site['invoices_documenttotals']['totaldocumentpayments'];
    }
    if ($masterclass->sitevars['customerid']!='') {
      $masterclass->site['invoices_customertotals']=$masterclass->allclasses['payments']->fetch_customer_payments($masterclass->sitevars['customerid']);
    }
  return;
  }
  
  function savedata() {
    global $masterclass;

    foreach ($masterclass->sitevars['post_action'] as $postaction) {
      switch ($postaction) {
       case 'invoices_submitpayment':
        if ($masterclass->site['captcha_post_auth']=='') { break; }
       
        if (isset($masterclass->sitevars['invoices_payment_date']) && $masterclass->sitevars['invoices_payment_date']!='') {
          $invoices_payment_date=$masterclass->sitevars['invoices_payment_date'];
        } else {
           $invoices_payment_date=date("Y-m-d"); 
        }
        
        $amt=$masterclass->allclasses['payments']->calc_payment_amt($masterclass->sitevars['invoices_payment_amount'],$masterclass->sitevars['invoices_paymenttype']);                
        
        if ($amt==0) { break; }
        $masterclass->sitevars['paymentid']=$masterclass->allclasses['payments']->add_payment($amt,$masterclass->sitevars['invoicesid'],$masterclass->sitevars['invoices_paymenttype'],$invoices_payment_date);
        
        if (money_format("%i",$amt)>(money_format("%i",$masterclass->site['invoices_documenttotals']['total_invoice_total'])-money_format("%i",$masterclass->site['invoices_paymenttotals']['totalpayments']))) {
          $amt=money_format("%i",$masterclass->site['invoices_documenttotals']['total_invoice_total'])-money_format("%i",$masterclass->site['invoices_paymenttotals']['totalpayments']);
        } else if (money_format("%i",$amt)==(money_format("%i",$masterclass->site['invoices_documenttotals']['total_invoice_total'])-money_format("%i",$masterclass->site['invoices_paymenttotals']['totalpayments']))) {
          $amt=money_format("%i",$amt);
        } else if (money_format("%i",$amt)<(money_format("%i",$masterclass->site['invoices_documenttotals']['total_invoice_total'])-money_format("%i",$masterclass->site['invoices_paymenttotals']['totalpayments']))) {
          $amt=money_format("%i",$amt);
        }

        $masterclass->allclasses['payments']->add_payment_documents($amt,$masterclass->sitevars['paymentid'],$masterclass->sitevars['invoicesid']);

        if ((money_format("%i",$masterclass->site['invoices_paymenttotals']['totalpayments'])+money_format("%i",$amt))>=money_format("%i",$masterclass->site['invoices_documenttotals']['total_invoice_total'])) {
          $masterclass->allclasses['payments']->mark_invoice_paid($masterclass->sitevars['invoicesid'],$invoices_payment_date);
        }

       break;
       case 'invoices_editquote':
         $dbinfo=array(
           'table'=>'invoices_documents',
           'trackingid'=>'invoicesid',
           'static'=>array(
             'documenttype'=>'quote'
           )
         );
         $masterclass->db_update($dbinfo);
         $masterclass->site['forward']=$masterclass->mainconfig['htmldirectory'].'invoices/quote.php?'.$masterclass->trackingvar_query();
       break;
       case 'invoices_savepdfasfile':
         $masterclass->site['forward']=$masterclass->mainconfig['htmldirectory'].'invoices/saveinvoice_pdf.php?nohtml=1&'.$masterclass->trackingvar_query();
       break;
       case 'invoices_sendpdfemail':
         $masterclass->site['forward']=$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice_emailpdf.php?'.$masterclass->trackingvar_query();
       break;
       case 'invoices_deleteinvoice':
         $dbinfo = array(
          'table'=>'invoices_documents',
          'trackingid'=>'invoicesid',
          'static'=>array($active=>'0')
         );
         $masterclass->uploaddata($dbinfo);
         
         $masterclass->site['forward']='window.close();';
       break;
      }
    }

  return;
  }

  function display_invoices_itemslist($containername='',$invoicesid='') {
    global $masterclass;
    $arrret=array();

    if (isset($_POST['rsargs']) && !empty($_POST['rsargs'])) {
      $containername=$_POST['rsargs'][1];
      $invoicesid=$_POST['rsargs'][2];
    }

    $listarray=$masterclass->allclasses['invoices']->fetch_invoices_items($invoicesid,'',' `datestamp` ');
    //$listdisplay=$masterclass->allclasses['invoices']->display_invoices_items($listarray);

    $ret='';
    if (empty($listarray->fields)) {
      $ret.='<table width="100%" align="center" cellpadding="0" cellspacing="0" class="sitebox_content">';
      $ret.='<tr valign="top">';
      $ret.='<td width="100%" valign="middle" style="padding-left:2px;text-align:center;">No Items</td>';
      $ret.='</tr>';
      $ret.='</table>';
    } else {
      $arrinvoicetotal=$masterclass->allclasses['invoices']->calc_invoicetotal($listarray);

      $ret.='<table width="100%" align="center" cellpadding="1" cellspacing="0" class="sitebox_content">';
      $ret.='<tr valign="top" class="sitebox_navigation">';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Item Count</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>SubTotal</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Tax</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Total</b></nobr></td>';
      $ret.='</tr>';
      $ret.='<tr valign="top" class="sitebox_navigation">';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>'.number_format($arrinvoicetotal['qty'],0).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['subtotal'],2).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['tax'],2).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['total'],2).'</b></nobr></td>';
      $ret.='</tr>';
      $ret.='</table>';


      $ret.='<table width="100%" align="center" cellpadding="1" cellspacing="0" class="sitebox_content">';
      $ret.='<tr valign="top" class="sitebox_navigation">';
      $ret.='<td style="border-right:1px solid;border-bottom:2px solid;"><nobr>&nbsp;<b>Name</b></nobr></td>';
      $ret.='<td width="15" style="text-align:center;border-right:1px solid;border-bottom:2px solid;"><nobr>&nbsp;<b>Qty</b></nobr></td>';
      $ret.='<td width="20" style="text-align:center;border-right:1px solid;border-bottom:2px solid;" ><nobr>&nbsp;<b>Price&nbsp;Per</b></nobr></td>';
      $ret.='<td width="30" style="text-align:center;border-right:1px solid;border-bottom:2px solid;"><nobr>&nbsp;<b>Tax&nbsp;</b></nobr></td>';
      $ret.='<td width="30" style="padding-right:1px;text-align:right;border-bottom:2px solid;"><nobr>&nbsp;<b>Total</b></nobr></td>';
      $ret.='</tr>';

      while (!$listarray->EOF) {
        $pname='';
        $profile='';
        if ($listedids!='') { $listedids.=','; } $listedids.=$listarray->fields['id'];

        if (date("Y-m-d")==date("Y-m-d",strtotime($listarray->fields['updated']))) {
          $pstamp = date("H:m:s",strtotime($listarray->fields['updated']));
        } else {
          $pstamp = date("Y-m-d",strtotime($listarray->fields['updated']));
        }

        if (preg_match('/^[0-9.-]+$/iD', $listarray->fields['qty'])) { $qty=$listarray->fields['qty']; } else { $qty=1; }
        if (preg_match('/^[0-9.-]+$/iD', $listarray->fields['cost'])) { $cost=$listarray->fields['cost']; } else { $cost=0; }
        if (preg_match('/^[0-9.-]+$/iD', $listarray->fields['taxpercent'])) { $taxpercent=$listarray->fields['taxpercent']; } else { $taxpercent=0; }

        if ($cost<0) { $polarity='-'; } else { $polarity='+'; }

        $subtotal=$qty*abs($cost);
        $taxcost=$subtotal*($taxpercent/100);
	    $total=$taxcost+$subtotal;

        $profile_mouseover='OnMouseOver="document.getElementById(\'invoices_itemsbrief'.$listarray->fields['id'].'\').className=\'sitebox_content_mouseover\';document.getElementById(\'invoices_itemsdesc'.$listarray->fields['id'].'\').className=\'sitebox_content_mouseover\';" OnMouseOut="document.getElementById(\'invoices_itemsbrief'.$listarray->fields['id'].'\').className=\'sitebox_content\';document.getElementById(\'invoices_itemsdesc'.$listarray->fields['id'].'\').className=\'sitebox_content\';"';
        //$profile_onclick_open='OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/edititem_default.php?invoicesitemid='.$listarray->fields['id'].'\',\'invoices_item'.$listarray->fields['id'].'\',\'480\',\'360\');"';
        //$removeitemclick='OnClick="if (confirm(\'Remove Item\\n'.$listarray->fields['name'].'?\')) { invoices_remove_item(\'invoices_removeitem\',\''.$listarray->fields['id'].'\'); }" ';

        $profile.='<tr id="invoices_itemsbrief'.$listarray->fields['id'].'" valign="top" class="sitebox_content" '.$profile_mouseover.'>';

        //$profile.='<td '.$removeitemclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/cross.png" border="0" title=" Delete "></td>';
        $profile.='<td style="text-align:left;cursor:pointer;padding-top:1px;padding-left:2px;padding-bottom:1px;cursor:pointer;border-right:1px solid;" '.$profile_onclick_open.'>'.$listarray->fields['name'].'</td>';

        //$qtyonclick='OnClick="hide(document.getElementById(\'invoices_edititem_qtytext_'.$listarray->fields['id'].'\'));show(document.getElementById(\'invoices_edititem_qtyinput_'.$listarray->fields['id'].'\'));document.getElementById(\'invoices_edititem_qty\').focus();"';
        $profile.='<td '.$qtyonclick.' width="10" style="text-align:left;padding-top:1px;padding-bottom:1px;cursor:pointer;border-right:1px solid;border-bottom:1px solid;" ><span id="invoices_edititem_qtytext_'.$listarray->fields['id'].'"><nobr>&nbsp;'.$listarray->fields['qty'].'</nobr></span><span id="invoices_edititem_qtyinput_'.$listarray->fields['id'].'" style="display:none;"><input OnFocus="this.select()" style="text-align:right;" type="text" size="3" name="invoices_edititem_qty" id="invoices_edititem_qty" value="'.$listarray->fields['qty'].'" class="sitebox_content"></span></td>';

        $profile.='<td width="20" style="text-align:right;padding-top:1px;padding-bottom:1px;cursor:pointer;border-right:1px solid;border-bottom:1px solid;" align="right" '.$profile_onclick_open.'><nobr>&nbsp;';
        if (preg_match('/^[0-9.-]+$/iD', $cost)) { $profile.='$'.number_format($cost,2); } else { $profile.=$cost; }
        $profile.='</nobr></td>';
        $profile.='<td width="30" style="text-align:right;padding-top:1px;padding-bottom:1px;cursor:pointer;border-right:1px solid;border-bottom:1px solid;" align="right" '.$profile_onclick_open.'><nobr>&nbsp;';
        if ($listarray->fields['taxed']=='1') {
	      if (preg_match('/^[0-9.-]+$/iD', $taxcost)) { $profile.='$'.number_format($taxcost,2).' '; } else { $profile.=$taxcost.' '; }
        } else {
	      $profile.='$0.00 ';
	    }
        $profile.='&nbsp;';
	    if ($listarray->fields['taxed']=='1') { $profile.=$taxpercent; } else { $profile.='0'; }
	    $profile.='%</nobr></td>';
        $profile.='<td width="30" style="text-align:right;padding-top:1px;padding-bottom:1px;cursor:pointer;border-bottom:1px solid;" align="right" '.$profile_onclick_open.'><nobr>&nbsp;';
        if (preg_match('/^[0-9.-]+$/iD', $total)) { $profile.='$'.number_format($total,2); } else { $profile.=$total; }
        $profile.='</nobr></td>';

        $profile.='</tr>';

        if ($listarray->fields['description']!='') {
	      $profile.='<tr id="invoices_itemsdesc'.$listarray->fields['id'].'" '.$profile_mouseover.' style="cursor:pointer;" '.$profile_onclick_open.'>';
	      $profile.='<td colspan="8" style="border-bottom:2px solid;">'.nl2br($listarray->fields['description']).'</td>';
	      $profile.='</tr>';
        } else {
	      $profile.='<tr id="invoices_itemsdesc'.$listarray->fields['id'].'" '.$profile_mouseover.' style="cursor:pointer;border-bottom:1px solid;" '.$profile_onclick_open.'>';
	      $profile.='<td colspan="8" style="border-bottom:2px solid;font-size:2px;">&nbsp;&nbsp;</td>';
	      $profile.='</tr>';
        }

        $ret.=$profile;
        
      $listarray->MoveNext();
      }
      $ret.='</table>';
      
      $ret.='<table width="100%" align="center" cellpadding="1" cellspacing="0" class="sitebox_content">';
      $ret.='<tr valign="top" class="sitebox_navigation">';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Item Count</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>SubTotal</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Tax</b></nobr></td>';
      $ret.='<td style="text-align:right;" ><nobr>&nbsp;<b>Total</b></nobr></td>';
      $ret.='</tr>';
      $ret.='<tr valign="top" class="sitebox_navigation">';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>'.number_format($arrinvoicetotal['qty'],0).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['subtotal'],2).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['tax'],2).'</b></nobr></td>';
      $ret.='<td style="text-align:right;border-bottom:1px solid;" ><nobr>&nbsp;<b>$'.number_format($arrinvoicetotal['total'],2).'</b></nobr></td>';
      $ret.='</tr>';
      $ret.='</table>';
    }

    $arrret['load_list']=$ret;
    $arrret['load_containername']=$containername;
    
    $masterclass->site['grabdata_vars']['load_list']=$ret;
    $masterclass->site['grabdata_vars']['load_containername']=$containername;

  return $arrret;
  }

  function display() {
    global $masterclass;

    $invoicetotals_listarray=$masterclass->allclasses['invoices']->fetch_invoices_totals($masterclass->sitevars['invoicesid']);
    $payment_listarray=$masterclass->allclasses['payments']->fetch_invoices_payments($masterclass->sitevars['invoicesid']);
  
    $arrdocumentdates=$masterclass->allclasses['invoices']->fetch_invoicesdocumentdates($masterclass->sitevars['invoicesid']);
    $masterclass->sitevars['invoices_documentdate']=$arrdocumentdates[0];
    $masterclass->sitevars['invoices_duedate']=$arrdocumentdates[1];
    $masterclass->sitevars['invoices_paid']=$arrdocumentdates[2];
    
    if ($masterclass->sitevars['invoices_documentdate']=='0000-00-00' || $masterclass->sitevars['invoices_documentdate']=='') { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d"); } else { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_documentdate'])); }
    if ($masterclass->sitevars['invoices_duedate']=='0000-00-00' || $masterclass->sitevars['invoices_duedate']=='') { $masterclass->sitevars['invoices_duedate']=''; } else { $masterclass->sitevars['invoices_duedate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_duedate'])); }
    if ($masterclass->sitevars['invoices_paid']=='0000-00-00' || $masterclass->sitevars['invoices_paid']=='') { $masterclass->sitevars['invoices_paid']=''; } else { $masterclass->sitevars['invoices_paid']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_paid'])); }

     if ($masterclass->sitevars['invoices_paid']!='' && $masterclass->sitevars['invoices_paid']!='0000-00-00') {
       $paymentdisplay='text';    
     } else {
       $paymentdisplay='inputtext';
     }
   
  $pageheader='<table width="100%" cellpadding="0" cellspacing="0" border="0" class="sitebox_pageheader"><tr>';
  $pageheader.='<td style="text-align:left;">'.$masterclass->sitevars['invoices_recipientlineone'].'</td>';
  $pageheader.='<td style="text-align:right;" width="25"><nobr>#'.$masterclass->sitevars['invoices_documentid'].'</nobr></td>';
  $pageheader.='</tr></table>';
  echo $masterclass->allclasses['websites']->sitebox_pageheader($pageheader,'');


  $nav='';
  $nav.='<div class="sitebox_navigation" style="width:100%;">';
  $nav.='<ul id="sitebox_navmenu">';
  $nav.='<li>PDF';
  $nav.='<ul>';
  $nav.='<li OnClick="window.open(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice_pdf.php?nohtml=1&'.$masterclass->trackingvar_query().'\',\'_blank\');">Show PDF</li>';
  $nav.='<li OnClick="window.open(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/saveinvoice_pdf.php?nohtml=1&'.$masterclass->trackingvar_query().'\',\'_blank\');">Save PDF as File</li>';
  $nav.='<li style="border-bottom:1px solid;" OnClick="window.open(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice_emailpdf.php?'.$masterclass->trackingvar_query().'\',\'_blank\');">Send PDF in Email</li>';
  $nav.='</ul>';
  $nav.='</li>';
  
  $nav.='<li OnClick="jsshowloadingscreen();add_post_action(\'invoices_editquote\');document.getElementById(\'frm\').submit();"><a>Edit Invoice</a></li>';
  /*
  if (isset($masterclass->sitevars['customerid']) && $masterclass->sitevars['customerid']>0) {
    $nav.='<li OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/customers_default.php?'.$masterclass->trackingvar_query().'\',\'customers_show'.$masterclass->sitevars['customerid'].'\',\'760\',\'480\');"><a>Show Customer</a></li>';
  }
  */
  if (isset($masterclass->sitevars['invoicesid']) && $masterclass->sitevars['invoicesid']>0) {
    $nav.='<li OnClick="if (confirm(\'DELETE\n\nAre You Sure?\')) { jsshowloadingscreen();add_post_action(\'invoices_deleteinvoice\');document.getElementById(\'frm\').submit(); }"><a>Delete</a></li>';
  }
  $nav.='<li OnClick="window.close();"><a>Close</a></li>';
  $nav.='</ul>';
  $nav.='<br style="clear: left;" />';
  $nav.='</div>';
  echo $nav;
?>
<table width="100%" cellpadding="2" cellspacing="5" border="0">
  <tr valign="top">
  <td width="40%">
<?php
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Invoice Information','');

  $calcstamp='calcduedate(\''.$masterclass->sitevars['invoices_documentdate'].'\',document.getElementById(\'invoices_net\'),document.getElementById(\'invoices_duedateframe\'),document.getElementById(\'invoices_duehidden\'),\'up\');';
?>
<table width="100%" align="center" cellpadding="2" cellspacing="0" class="sitebox_content">
  <tr valign="top">
    <td width="50" valign="middle" style="padding-left:2px;"><nobr>Doc #:&nbsp;</nobr></td>
    <td width="95%"><?php echo $masterclass->allclasses['websites']->disp_text('invoices_documentid');?>&nbsp;</td>
  </tr>
   <tr valign="top">
     <td width="50" valign="middle" style="padding-left:2px;"><nobr>Date:&nbsp;</nobr></td>
     <td width="95%" valign="middle" id="invoices_docdateframe"><?php echo $masterclass->allclasses['websites']->disp_text('invoices_documentdate');?>&nbsp;</td>
   </tr>
   <tr valign="absmiddle">
    <td width="50" valign="middle" style="padding-left:2px;"><nobr>Terms:&nbsp;</nobr></td>
    <td width="95%" colspan="3"><?php if ($masterclass->sitevars['invoices_net']=='0') { echo 'Upon Receipt'; } else { echo 'Net '.$masterclass->allclasses['websites']->disp_text('invoices_net'); }?>&nbsp;</td>
   </tr>
   <tr valign="top">
     <td width="50" valign="middle" style="padding-left:2px;"><nobr>Due&nbsp;Date:&nbsp;</nobr></td>
     <td width="95%" valign="middle" id="invoices_duedateframe"><?php echo $masterclass->allclasses['websites']->disp_text('invoices_duedate');?>&nbsp;</td>
    </tr>
   <tr valign="top">
     <td width="50" valign="middle" style="padding-left:2px;"><nobr>Paid:&nbsp;</nobr></td>
     <td width="95%" valign="middle"><?php echo $masterclass->allclasses['websites']->disp_text('invoices_paid');?>&nbsp;</td>
    </tr>
  </table>
<?php
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
  echo $masterclass->allclasses['websites']->sitebox_vspacer();
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Recipient','');
?>
<table width="100%" align="center" cellpadding="0" cellspacing="1" class="sitebox_content">
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_recipientlineone');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_recipientlinetwo');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_recipientlinethree');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_recipientlinefour');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_recipientlinefive');?></td>
  </tr>
  </table>
<?php
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
  echo $masterclass->allclasses['websites']->sitebox_vspacer();
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Company','');
?>
<table width="100%" align="center" cellpadding="0" cellspacing="1" class="sitebox_content">
  <tr valign="top">
     <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_companylineone');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_companylinetwo');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_companylinethree');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_companylinefour');?></td>
  </tr>
  <tr valign="top">
    <td width="95%">&nbsp;&nbsp;<?php echo $masterclass->allclasses['websites']->disp_text('invoices_companylinefive');?></td>
  </tr>
</table>
<?php
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
?>
</td><td width="60%">
<?php
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Payment Information','');
  $totalpaymentamt=0;
  $totals_array=array('total_payments'=>0);
  
  if ($payment_listarray) {
    $totals_array['total_payments']=$masterclass->allclasses['payments']->calc_invoices_payments_total($payment_listarray);
      
    echo '<table width="100%" align="center" cellpadding="0" cellspacing="0" class="sitebox_content">';
      echo '<tr valign="top" class="sitebox_navigation" >';
      echo '<td class="sitebox_navigation" width="50" valign="middle" style="padding-left:2px;padding-right:2px;text-align:left;border-bottom:1px solid;"><nobr>Payment</nobr>&nbsp;</td>';
      echo '<td class="sitebox_navigation" width="50" valign="middle" style="padding-left:2px;padding-right:2px;text-align:left;border-bottom:1px solid;"><nobr>Applied</nobr></td>';
      echo '<td class="sitebox_navigation" valign="middle" style="padding-left:2px;text-align:left;border-bottom:1px solid;"><nobr>Type</nobr>&nbsp;</td>';
      echo '<td class="sitebox_navigation" width="50" valign="middle" style="padding-left:2px;text-align:right;border-bottom:1px solid;"><nobr>Date</nobr>&nbsp;</td>';
      echo '</tr>';
    while (!$payment_listarray->EOF) {
      echo '<tr valign="top" class="sitebox_content" style="cursor:pointer;" OnMouseOver="this.className=\'sitebox_content_mouseover\';" OnMouseOut="this.className=\'sitebox_content\';" >';
      echo '<td class="sitebox_content" width="50" valign="middle" style="padding-left:2px;padding-right:2px;text-align:left;border-bottom:1px solid;"><nobr>$'.number_format($payment_listarray->fields['paymentamt'],2).'</nobr>&nbsp;</td>';
 
 /*
      $payment_percent=((money_format("%i",$payment_listarray->fields['invoicepayment']) / money_format("%i",$invoicetotals_listarray['total']) * 100));
      if ($payment_percent=='100' && money_format("%i",$payment_listarray->fields['invoicepayment'])<money_format("%i",$invoicetotals_listarray['total'])) {
        $payment_percent='99';
      }
      if (($payment_percent=='' || $payment_percent=='0') && money_format("%i",$payment_listarray->fields['invoicepayment'])>0) {
        $payment_percent='1';
      }
      $payment_percent.='%';
*/
      echo '<td class="sitebox_content" width="50" valign="middle" style="padding-left:2px;padding-right:2px;text-align:left;border-bottom:1px solid;"><nobr>$'.number_format($payment_listarray->fields['invoicepayment'],2).'&nbsp;'.$payment_percent.'</nobr></td>';
      echo '<td class="sitebox_content" valign="middle" style="padding-left:2px;text-align:left;border-bottom:1px solid;"><nobr>'.$payment_listarray->fields['paymenttype'].'&nbsp;';
      switch (strtolower($payment_listarray->fields['paymenttype'])) {
       case 'check':
        if ($payment_listarray->fields['checknumber']!='') {
          echo '#'.$payment_listarray->fields['checknumber'];
        }
       break;
      }
      echo '</nobr>&nbsp;</td>';
      echo '<td class="sitebox_content" width="50" valign="middle" style="padding-left:2px;text-align:right;border-bottom:1px solid;"><nobr>'.$payment_listarray->fields['paymentdate'].'</nobr>&nbsp;</td>';
      echo '</tr>';
    
    $payment_listarray->MoveNext();
    }
    echo '</table>';
  }
  
  
  if ((money_format("%i",$invoicetotals_listarray['total'])-money_format("%i",$totals_array['total_payments']))>0) {
    if ($masterclass->sitevars['invoices_payment_date']=='' || $masterclass->sitevars['invoices_payment_date']=='0000-00-00') { $masterclass->sitevars['invoices_payment_date']=date("Y-m-d"); } else { $masterclass->sitevars['invoices_payment_date']=''; }
    if ($masterclass->sitevars['invoices_payment_amount']=='' || $masterclass->sitevars['invoices_payment_amount']=='0') {
      $masterclass->sitevars['invoices_payment_amount']=number_format((money_format("%i",$invoicetotals_listarray['total'])-money_format("%i",$totals_array['total_payments'])),2);
    }
    
    echo $masterclass->allclasses['payments']->display_payment();
    echo '<div style="text-align:center;padding:3px;"><input type="button" class="button" OnClick="captcha_action(\'add_post_action(\\\'invoices_submitpayment\\\');document.getElementById(\\\'frm\\\').submit();\');" value="Submit Payment"></div>';
  }
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
  echo $masterclass->allclasses['websites']->sitebox_vspacer(5);
/* Start Window */
  echo $masterclass->allclasses['websites']->sitebox_start('100%');
  echo $masterclass->allclasses['websites']->sitebox_headline('Invoice Items','');
  
  $unquieid='invoice_items_container';
  $invoices_itemsclick='update_'.$unquieid.'();';

  $runcmdpre='';
  $cngpg_cmd="invoices_itemslist('display_invoices_itemslist','invoice_items_container','".$masterclass->sitevars['invoicesid']."');";
  $runcmdpost=$cngpg_cmd;
  
  $listdata=$this->display_invoices_itemslist($unquieid,$masterclass->sitevars['invoicesid']);
?>
<div id="<?php echo $unquieid; ?>"><?php echo $listdata['load_list']; ?></div>
<script language="javascript">
  var <?php echo $unquieid; ?>_listview='';
  function update_<?php echo $unquieid;?>() {
    <?php echo $cngpg_cmd; ?>
  }
</script>
<?php
  echo $masterclass->allclasses['websites']->sitebox_end();
/* End Window */
?>
</td></tr></table>
<?php

  return;
  }
}
?>